package com.intsig.imageprocess;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

public class WelcomeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.cui_welcome_activity);
		// new Handler().postDelayed(r, 1000);// 1秒后关闭，并跳转到主页面

		final TextView textView = (TextView) findViewById(R.id.clickme_id);
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				count++;
				textView.setText(getResources().getString(R.string.click_me_again) + count);

				clickFunc();
			}
		});
	}

	int count = 0;

	public void clickFunc() {
		// TODO Auto-generated method stub

		Intent intent = new Intent();

		intent.setClass(WelcomeActivity.this, ImageScannerActivity.class);

		intent.putExtra(ImageScannerActivity.EXTRA_KEY_APP_KEY, APPKEY);

		intent.putExtra(ImageScannerActivity.EXTRA_IMPORT_IMG_TYPE,
				ImageScannerActivity.IMPORT_FROM_CAMERA);

		
		intent.putExtra(ImageScannerActivity.EXTRA_TRIM_IMAGE_MAXSIDE, 1600);
//		intent.putExtra(ImageScannerActivity.EXTRA_KEY_ENHANCE_MODE_INDEX,
//				ImageScannerActivity.ENHANCE_LINER_FUTURE_MODE);
		String mRootPath = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		String outPutFilePath = mRootPath + File.separator + "fox"
				+ File.separator + "fox1" + File.separator + "111fox.jpg"; // 替换成输出的图片路径

		String inputFilePathString = mRootPath + File.separator + "Android"
				+ File.separator + "data" + File.separator
				+ "com.intsig.BizCardReader" + File.separator + "files"
				+ File.separator + "cardinfo" + File.separator + "company"
				+ File.separator + "fox125.jpg"; // 替换成输入的图片路径

		intent.putExtra(ImageScannerActivity.EXTRA_KEY_INPUTFILE_DATA_PATH,
				inputFilePathString);
		intent.putExtra(ImageScannerActivity.EXTRA_KEY_RESULT_DATA_PATH,
				outPutFilePath);
		intent.putExtra(ImageScannerActivity.EXTRA_POSITON,
				getIntent().getIntExtra(ImageScannerActivity.EXTRA_POSITON,0));
		startActivityForResult(intent, IMAGE_SCANNER_CODE);
//		 startActivity(intent);
//		 finish();

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK && requestCode == IMAGE_SCANNER_CODE) {

			final String outputPath = data
					.getStringExtra(ImageScannerActivity.EXTRA_KEY_RESULT_DATA_PATH);

			if (!TextUtils.isEmpty(outputPath)) {
				new AlertDialog.Builder(this)
						.setTitle(R.string.a_label_save)
						.setMessage(outputPath)
						.setNegativeButton(R.string.a_label_cancel, null)
						.setPositiveButton(R.string.a_label_open,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Uri uri = Uri.fromFile(new File(
												outputPath));
										Intent intent = new Intent(
												Intent.ACTION_VIEW);
										intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
										intent.putExtra(Intent.EXTRA_STREAM,
												uri);
										intent.setDataAndType(uri, "image/*");
										try {
											startActivity(intent);
										} catch (Exception e) {
											Toast.makeText(
													WelcomeActivity.this,
													R.string.a_msg_not_available_application,
													Toast.LENGTH_SHORT).show();
										}

									}
								}).create().show();
			}
		} else if (resultCode == RESULT_CANCELED
				&& requestCode == IMAGE_SCANNER_CODE) {
			// 识别失败或取消
			// Log.d(TAG, "识别失败或取消");
			// if (data != null) {
			// /**
			// * 101 包名错误 102 appKey错误 103 超过时间限制 104 达到设备上限 201 签名错误 202 其他错误
			// * 203 服务器错误 204 网络错误 205 包名/签名错误
			// */
			// int error_code = data.getIntExtra(
			// ISCardScanActivity.EXTRA_KEY_RESULT_ERROR_CODE, 0);
			// Toast.makeText(
			// this,
			// "Error:" + error_code + "\nMsg:"
			// + CommonUtil.commentMsg(error_code),
			// Toast.LENGTH_SHORT).show();
			// }

		}

	};

	private static String APPKEY = "63be74bfdbcac018b4852c0b10-vagfvt";
	private static int IMAGE_SCANNER_CODE = 1001;
}
